﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using RAE.Logica.Controladores;
using System.Text.RegularExpressions;

namespace RAE.GUI.Info
{
    /// <summary>
    /// Interaction logic for EstadisticaAjedrecistas.xaml
    /// </summary>
    public partial class EstadisticaAjedrecistas : Window
    {
        public EstadisticaAjedrecistas()
        {
            InitializeComponent();
            cargar();
        }

        private void cargar()
        {
            GridEstadisticas.ItemsSource = Aplicacion.ajedrecistas;
            GridEstadisticas.Items.Refresh();
        }
        private void filtrado()
        {
            try
            {
                if ((bool)radioListaCompleta.IsChecked) cargar();

                textCedula.IsEnabled = (bool)radioCedula.IsChecked;
                if ((bool)radioCedula.IsChecked) filtroCedula();

                textNombre.IsEnabled = (bool)radioNombre.IsChecked;
                if ((bool)radioNombre.IsChecked) filtroNombre();
            }
            catch (Exception a)
            { 
            }

        }
        private void filtroCedula()
        {
            GridEstadisticas.ItemsSource = Aplicacion.ajedrecistas.Select(a => a).Where(a => Regex.IsMatch(a.cedula.Trim(), textCedula.Text, RegexOptions.IgnoreCase));
            GridEstadisticas.Items.Refresh();  
        }
        private void filtroNombre()
        {
            GridEstadisticas.ItemsSource = Aplicacion.ajedrecistas.Select(a => a).Where(a => Regex.IsMatch(a.nombre.Trim(), textNombre.Text, RegexOptions.IgnoreCase));
            GridEstadisticas.Items.Refresh();        
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Checked(object sender, RoutedEventArgs e)
        {
            filtrado();
        }

        private void FiltroPorNombre(object sender, TextChangedEventArgs e)
        {
            filtroNombre();
        }
        private void FiltroPorCedula(object sender, TextChangedEventArgs e)
        {
            filtroCedula();
        }

        private void textCedula_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex regex = new Regex("^(-)?\\d+$|^-?$");
                e.Handled = !(regex.IsMatch(e.Text));
            }
            catch (Exception f)
            { }

        }

        private void textNombre_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Regex regex = new Regex("[a-zA-Z]+$");
                e.Handled = !(regex.IsMatch(e.Text));
            }
            catch (Exception f)
            { }
        }
    }
}
